#include "tcp_socket.h"
#include <iostream>
#include <stdint.h>
#include <netinet/in.h>
#include "runnable.h"
#include "thread_pool.h"
#include "work_thread.h"
using namespace NetKit;
using namespace std;
using namespace CThread;

class MyRunner : public Runnable
{
        void run()
        {
		TcpSocket sock;
		int ret = sock.Connect("localhost", 9000);
		if ( ret ==0 )
			cout << "connect localhost 9000 success" << endl;
		else
			cout << "connect localhost 9000 fail" << endl;
		int len = 10;
		/*
		uint32_t sendLen = htonl(len);		
		ret = sock.Send((char*)&sendLen, sizeof(sendLen));
		if ( ret > 0 )
			cout << "send header len success" << endl;
		*/
		ret = sock.Send("0123456789",len);
		if ( ret > 0 )
			cout << "send content success" << endl;
		
		//sleep(5);
		char szBuf[1024];
		sock.Recv(szBuf, 1024);
		cout << "recv:" << szBuf << endl;

        }
};

int main()
{
        uint32_t aa = 0;
        uint32_t thread=30;
	WorkThreadFactory* pThreadFactory = new WorkThreadFactory();
        ThreadPool pool(pThreadFactory, thread, 10240);
        pool.start();
        printf("ready to add task\n");
        for ( int i=0; i<200; i++ )
        {
        	MyRunner* pRunner = new MyRunner();
	        printf("add task %d\n", i);
                pool.add(pRunner);
        }
        printf("add task finished\n");
	return 0;
}
